<?php

namespace app\controllers\admin;

use yii;
use app\commands\ErrorMsg;
use app\commands\Helper;
use app\commands\Validator;
use app\Services\admin\UserService;
use yii\web\Controller;
use linslin\yii2\curl;
use Qcloud\Sms\SmsSingleSender;



class TestController extends Controller
{
    public $req = array();
    public $user = array();

    /**
     * 安全 校验 屏蔽
     * 预处理  权限等等操作处理
     */
    public function beforeAction($action){
        $this->req = Helper::in(array_merge($_POST, $_GET));
//        $this->user = Helper::checksign($this->req);
        return true;
    }

    /**
     * 登录
     *
     */
    public function actionLogin(){
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'username'  => "required",
            'password'  => "required"
        );
        Validator::validator($req, $rules,ErrorMsg::ERROR_MSG);
        $res = UserService::getInstance()->login($req);
        Helper::right_out($req,$res);
    }


    /**
     * 生成token
     */
    public function actionJwt(){
        $request = Yii::$app->getRequest();
        $signer = new Sha256();//使用Sha256加密，常用加密方式有Sha256,Sha384,Sha512
        $time = time();
        $tokenBuilder = (new Builder())
            ->issuedBy($request->getHostInfo())// 设置发行人
            ->permittedFor(isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '')// 设置接收
            ->identifiedBy(Yii::$app->security->generateRandomString(10), true)// 设置id
            ->issuedAt($time)// 设置生成token的时间
            ->canOnlyBeUsedAfter($time)// 设置token使用时间(实时使用)
            ->expiresAt($time + 3600);//设置token过期时间
        //定义自己所需字段
        $user = ['user_name' => '测试', 'user_no' => '001'];
        $tokenBuilder->withClaim('user', $user);
        $tokenBuilder->withClaim('ceshi', '测试字段');
        //使用Sha256加密生成token对象，该对象的字符串形式为一个JWT字符串
        $token = $tokenBuilder->getToken($signer, new Key('jwt_secret'));
        echo (string) $token;
    }


    /**
     * 发送短信
     */
    public function actionSendSms(){
        try {
            $appid = Yii::$app->params['Sms']['appid'];
            $appkey = Yii::$app->params['Sms']['appkey'];
            $templateId = Yii::$app->params['Sms']['register_templateId'];
            $smsSign = Yii::$app->params['Sms']['smsSign'];

            $ssender = new SmsSingleSender($appid, $appkey);
            $params = ["5678",2];
            $result = $ssender->sendWithParam("86", '13057695126', $templateId,
                $params, $smsSign, "", "");
            $rsp = json_decode($result);
            echo $result;
        } catch(\Exception $e) {
            echo var_dump($e);
        }
    }


    /**
     * fasongyoujian
     */
    public function actionSendEmail(){
        $mail= Yii::$app->mailer->compose();
        $mail->setTo("1562663273@qq.com");
        $mail->setSubject(date("Y-m-d")."安播统计报告");
        $mail->setHtmlBody("<br>附件为本次报告，请查收！");
        if($mail->send()){
            echo "成功";
        }else{
            echo "失败";
        }

    }

}
